package softswitch.service;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import softswitch.model.CallSummaryRouteGroup;
import softswitch.model.Circuit;
import softswitch.model.RouteGroup;
import util.DateUtil;


@Stateless
public class CallSummaryRouteGroupService {

	@EJB
	DateUtil dateUtil;
	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	

	public CallSummaryRouteGroup getById(Integer id){	
		return (CallSummaryRouteGroup) em.find(CallSummaryRouteGroup.class, id);
	}
	
	public CallSummaryRouteGroup save(CallSummaryRouteGroup callSummaryRouteGroup) {
    	em.persist(callSummaryRouteGroup);
    	return callSummaryRouteGroup;
    }
	
	public CallSummaryRouteGroup update(CallSummaryRouteGroup callSummaryRouteGroup) {
    	return em.merge(callSummaryRouteGroup);
    }
    
	public void delete(Integer id) {
    	em.remove(em.find(CallSummaryRouteGroup.class, id));	
    }
    
    //----------------------------------------------------------------------
	
	public void deleteByMonth(Integer year, Integer month, Circuit circuit ) {	
		String sql = "DELETE FROM CallSummaryRouteGroup e WHERE e.year=:year AND e.month=:month";
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		Query query = em.createQuery(sql);
		query.setParameter("year", year);
		query.setParameter("month", month);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
        query.executeUpdate();   
    }

    public CallSummaryRouteGroup getByDate(Date calldate, Circuit circuit, RouteGroup routegroup) {	
    	String sql = "SELECT e FROM CallSummaryRouteGroup e  WHERE e.calldate=:calldate ";
		
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		if(routegroup!=null){
			sql = sql + " AND e.routegroup=:routegroup ";
		}
		
		Query query = em.createQuery(sql);
		query.setParameter("calldate", calldate);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
		if(routegroup!=null){
			query.setParameter("routegroup", routegroup);
		}
		try{
			return (CallSummaryRouteGroup) query.getSingleResult();
		}
		catch(Exception e){
			return null;
		}
    }
    
    public CallSummaryRouteGroup getByMonth(Integer year, Integer month, Circuit circuit, RouteGroup routegroup) {
    	Date yearmonth = dateUtil.customDate(year, month, null, null, null, null, null);
    	return getByDate(yearmonth,circuit,routegroup);
    }
    
    
    @SuppressWarnings("unchecked")
	public List<CallSummaryRouteGroup> listByDateRange(Date startdate, Date enddate, Circuit circuit, RouteGroup routegroup) {	
    	String sql = "SELECT e FROM CallSummaryRouteGroup e  WHERE e.calldate BETWEEN :startdate AND :enddate ";
		
		if(circuit!=null){
			sql = sql + " AND e.circuit=:circuit ";
		}
		if(routegroup!=null){
			sql = sql + " AND e.routegroup=:routegroup ";
		}
		
		Query query = em.createQuery(sql);
		query.setParameter("startdate", startdate);
		query.setParameter("enddate", enddate);
		if(circuit!=null){
			query.setParameter("circuit", circuit);
		}
		if(routegroup!=null){
			query.setParameter("routegroup", routegroup);
		}
		try{
			return query.getResultList();
		}
		catch(Exception e){
			return null;
		}
    }
        
}


